package finger.web;

import finger.domain.Cart;
import finger.domain.Order;
import finger.domain.OrderItem;
import finger.domain.User;
import finger.service.OrderService;
import finger.service.impl.OrderServiceImpl;
import finger.utils.JDBCUtils;
import finger.utils.WebUtils;
//import finger.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {
    OrderService service = new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        int id = user.getId();
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        String order = null;
        order = service.createOrder(cart, id);
//        req.setAttribute("orderId", order);
//        req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
        req.getSession().setAttribute("orderId", order);
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }

    protected void myOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        int id = user.getId();
        List<Order> orders = service.myOrders(id);
        req.setAttribute("orders", orders);
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req, resp);
    }

    protected void orderDetails(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        List<OrderItem> items = service.orderDetails(orderId);
        req.setAttribute("items", items);
        req.setAttribute("referer", req.getHeader("referer"));
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req, resp);
    }

    protected void allOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> orders = service.queryAllOrders();
        req.setAttribute("orders", orders);
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
    }

    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        service.sendOrder(orderId);
        req.getRequestDispatcher("orderServlet?action=allOrders").forward(req, resp);
    }

    protected void receiveOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        service.receiveOrder(orderId);
        resp.sendRedirect("orderServlet?action=myOrders");
    }

}
